﻿using System.IO;
using System.Linq;
using PSE.Deployment.Engine;
using PSE.Deployment.Engine.Enums;
using PSE.Deployment.Engine.Interfaces;
using PSE.Deployment.SetupBase.UI;
using PSE.Deployment.Util;

namespace PSE.Deployment.SetupBase.Steps
{
    public class CheckInstalationSource : BaseStep
    {
        public CheckInstalationSource(IStepGroup parent)
            : base(parent)
        {
            this.Id = "CheckInstalationSource";
            this.Description = "Verificação da origem da instalção";

            this.ErrorBehavior = StepErrorBehavior.CancelParent;
            this.NavigationBehavior = StepNavigationBehavior.NextCancel;

            this.ResetProgress(1);
        }

        public override void Execute()
        {
            this.ResetProgress(1);

            this.IncrementProgress(Description);

            if (SetupMainFactory.GetInstance().Parameter.SetupMode != PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Uninstall && 
                !Util.Windows.CheckInstallationSource(SetupMainFactory.GetInstance().Parameter.InstalationSource) &&
                SetupMainFactory.GetInstance().Parameter.SetupConfiguration.SystemInfo.Groups.SelectDeep(g => g.SubGroups).SelectDeep(g => g.Dependencies).Any(g => g.Install == true && g.Installed == false))
            {
                InstalationSource _instalationSource = new InstalationSource();

                if (!_instalationSource.ShowDialog().GetValueOrDefault())
                {
                    this.Parent.Cancel();
                }
            }
        }


        public override void Undo()
        {
            if (SetupMainFactory.GetInstance().Parameter.SetupMode == Parameter.Enums.SetupMode.Install || SetupMainFactory.GetInstance().Parameter.SetupMode == Parameter.Enums.SetupMode.Change)
            {
                this.DecrementProgress(Description);
            }
        }
    }
}
